package mm.apk.libmm.exts

/**
 * Descriptions：
 * StartVersion：
 * <p>
 * Author：ChenME
 * Date：2019-11-15
 * Email：ibelieve1210@163.com
 */


/**
 * 判断字符串是否为空
 */
fun String?.isNil(): Boolean = null == this || this.trim().isEmpty()
fun String?.isNotNil(): Boolean = !this.isNil()
fun String?.isNilOrNull(): Boolean = null == this || this.trim().isEmpty() || this.toLowerCase() == "null"
fun String?.isNotNilOrNull(): Boolean = !this.isNilOrNull()


/**
 * 判断字符串是否为空
 */
fun CharSequence?.isNil(): Boolean = null == this || this.isEmpty()
fun CharSequence?.isNotNil(): Boolean = !isNil()
fun CharSequence?.isNilOrNull(): Boolean = null == this || this.isEmpty() || this.toString().toLowerCase() == "null"
fun CharSequence?.isNotNilOrNull(): Boolean = !isNilOrNull()

/**
 * 取一个不为 null 的字符串
 */
fun String?.notNilText(default: String = ""): String = if (this.isNil()) default else this!!
fun String?.notNilOrNullText(default: String = ""): String = if (this.isNilOrNull()) default else this!!


/**
 * 取一个不为 null 的字符串
 */
fun CharSequence?.notNilText(default: String = ""): String = this?.toString() ?: default
fun CharSequence?.notNilOrNullText(default: String = ""): String = if (this.isNilOrNull()) default else this.toString()

/**
 * 格式化富文本为 H5
 */
fun String?.richText(title: String? = null): String {
    return """
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body {
            font-size: 16px;
        }
    </style>
</head>
<body>
    <script type='text/javascript'>
        window.onload = function () {
            var img = document.getElementsByTagName('img');
            for (var p in img) {
                img[p].style.width = '100%';
                img[p].style.height = 'auto'
            }
        }
    </script>
    ${if (null != title) "<h3>$title</h3>" else ""}
    ${if (null != this) "$this" else ""}
</body>
</html>
            """.trimIndent().replace("style=\"white-space: nowrap;\"", "")
}


/**
 * 将字符串转换成整型，如果异常则返回默认值
 */
fun String?.toIntNoErr(defaultInt: Int = 0): Int {
    if (null == this) {
        return defaultInt
    }
    return try {
        this.toInt()
    } catch (e: Exception) {
        e.printStackTrace()
        defaultInt
    }
}


/**
 * 将字符串转换成整型，如果异常则返回默认值
 */
fun String?.toLongNoErr(default: Long = 0): Long {
    if (null == this) {
        return default
    }
    return try {
        this.toLong()
    } catch (e: Exception) {
        e.printStackTrace()
        default
    }
}

/**
 * 将字符串转换 Double，如果异常则返回默认值
 */
fun String?.toDoubleNoErr(default: Double = 0.0): Double {
    if (null == this) {
        return default
    }
    return try {
        this.toDouble()
    } catch (e: Exception) {
        e.printStackTrace()
        default
    }
}


/**
 * 如果金钱后面没有角分，则保留整数
 */
fun String?.money2IntIfCan(): String {
    if (null == this) {
        return ""
    }
    if (this.contains(".")) {
        val tmp = this.split(".")
        if (tmp[1].toLongNoErr() == 0L) {
            return tmp[0]
        }
        return this
    }
    return this
}


/**
 * 最长 N 个字，之后省略
 */
fun String?.maxLetter(maxLength: Int = 8): String {
    if (null == this || isEmpty()) {
        return ""
    }
    if (this.length <= maxLength) {
        return this
    }
    return "${this.substring(0 until maxLength)}..."
}
